package com.fzk.blog.settings.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fzk.blog.bean.Tag;
import com.fzk.blog.bean.vo.TagInfo;
import com.fzk.blog.blog.service.IRedisService;
import com.fzk.blog.exception.MyException;
import com.fzk.blog.mapper.TagMapper;
import com.fzk.blog.settings.service.ITagService;
import com.fzk.blog.utils.MyJsonUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author fzk
 * @date 2021-08-17 14:42
 */
@Service
public class TagService implements ITagService {
    @Resource
    private TagMapper tagMapper;
    @Resource
    private IRedisService redisService;

    @Override
    public List<TagInfo> getTagInfoList() throws JsonProcessingException {
        List<TagInfo> tagInfoList = null;
        // 1.查redis
        String json = redisService.get("tagInfoList");
        // 2 没有的话，从数据库拿，并放入redis
        if (json == null) {
            tagInfoList = tagMapper.getTagInfoList();
            redisService.setEx("tagInfoList", MyJsonUtil.writeToString(tagInfoList));
        } else
            tagInfoList = MyJsonUtil.toBeanArray(json, TagInfo.class);
        return tagInfoList;
    }

    @Override
    public TagInfo getTagInfoById(int id) throws JsonProcessingException {
        TagInfo tagInfo = null;
        // 1.从redis拿
        String json = redisService.get("tagInfo:id:" + id);
        // 2 没有的话，从数据库拿，并放入redis
        if (json != null)
            tagInfo = MyJsonUtil.toBean(json, TagInfo.class);
        else {
            tagInfo = tagMapper.getTagInfoById(id);
            if (tagInfo == null)
                throw new MyException(400, "id不存在：" + id);
            redisService.setEx("tagInfo:id:" + id, MyJsonUtil.writeToString(tagInfo));
        }
        return tagInfo;
    }

    @Override
    @Transactional//开启事务
    public TagInfo insertTag(Tag tag, int loginId) throws JsonProcessingException {
        // 1.插入数据准备
        tag.setCreateBy(loginId);
        tag.setEditBy(loginId);
        tag.setFlag(0);

        // 2.检查是否已经有相同名称的tag
        List<Tag> tags = tagMapper.selectByName(tag.getName());
        if (tags.size() != 0)
            throw new MyException(400, "已经存在此标签名：" + tag.getName());

        // 3.插入
        int i = tagMapper.insert(tag);
        if (i != 1)
            throw new MyException(500, "插入失败");

        // 4.返回tagInfo
        TagInfo tagInfo = tagMapper.getTagInfoById(tag.getId());

        // 5. 放入redis
        redisService.setEx("tagInfo:id:" + tag.getId(), MyJsonUtil.writeToString(tagInfo));

        // 6. 清redis
        redisService.delete("tagInfoList");

        return tagInfo;
    }

    @Override
    @Transactional//开启事务
    public TagInfo updateTag(Tag tag, int loginId) throws JsonProcessingException {
        // 1.完善更新信息
        tag.setEditBy(loginId);

        // 2.检查是否存在相同的tagName
        List<Tag> tags = tagMapper.selectByName(tag.getName());
        if (tags.size() != 0) {
            if (!tags.get(0).getId().equals(tag.getId()))
                throw new MyException(400, "已经存在此标签名：" + tag.getName());
        }

        // 3.更新
        int i = tagMapper.updateById(tag);
        if (i != 1)
            throw new MyException(500, "更新失败");

        // 4.返回tagInfo
        TagInfo tagInfo = tagMapper.getTagInfoById(tag.getId());

        // 5.放入redis
        redisService.setEx("tagInfo:id:" + tag.getId(), MyJsonUtil.writeToString(tagInfo));

        //6.清redis
        redisService.delete("tagInfoList");
        return tagInfo;
    }

    @Override
    @Transactional//开启事务
    public TagInfo deleteTag(int tagId, int loginId) {
        // 1.查询tagInfo
        TagInfo tagInfo = tagMapper.getTagInfoById(tagId);
        if (tagInfo == null)
            throw new MyException(400, "参数错误,不存在id：" + tagId);

        // 2.逻辑删除
        int i = tagMapper.logicalDeleteById(tagId, loginId);
        if (i != 1)
            throw new MyException(500, "删除失败");

        // 3.清redis
        redisService.delete("tagInfoList", "tagInfo:id:" + tagId);
        return tagInfo;
    }
}
